The Binomial distribution visualize the Binomial distribution X~B(n,p) think of n coin tosses, sum number of "face"

In [1]:
options(repr.plot.width=10, repr.plot.height=6.5)   # this command just formats the size of the figures. Adapt to view them nicely
                                                    # in your browser.
In [2]:
n <- 50
p <- 0.80

k <- seq(0,n,by=1)                       #Binomial: x can take any value k between 0 and n.
f_k <- dbinom(x = k,size = n,prob = p)   # compute pdf of B(n,k) with dbinom
plot(k,f_k, ylab = "PDF")
No description has been provided for this image
In [3]:
# Show numerical pdf

f_k
  1. 1.12589990684261e-35
  2. 2.25179981368521e-33
  3. 2.20676381741155e-31
  4. 1.41232884314337e-29
  5. 6.63794556277396e-28
  6. 2.44276396710079e-26
  7. 7.32829190130244e-25
  8. 1.84254196375602e-23
  9. 3.96146522207543e-22
  10. 7.39473508120754e-21
  11. 1.21273655331804e-19
  12. 1.76398044118988e-18
  13. 2.29317457354682e-17
  14. 2.68125027060861e-16
  15. 2.83446457178623e-15
  16. 2.72108598891478e-14
  17. 2.38095024030043e-13
  18. 1.90476019224034e-12
  19. 1.39682414097625e-11
  20. 9.41018368657688e-11
  21. 5.83431388567769e-10
  22. 3.33389364895867e-09
  23. 1.75787119672365e-08
  24. 8.56006843621956e-08
  25. 3.85203079629882e-07
  26. 1.6024448112603e-06
  27. 6.1632492740781e-06
  28. 2.19137751967221e-05
  29. 7.20024042178015e-05
  30. 0.000218490054178156
  31. 0.000611772151698834
  32. 0.00157876684309377
  33. 0.0037495712523477
  34. 0.00818088273239499
  35. 0.01636176546479
  36. 0.0299186568499017
  37. 0.0498644280831694
  38. 0.0754704857474997
  39. 0.10327540154921
  40. 0.127108186522105
  41. 0.139819005174315
  42. 0.136408785535918
  43. 0.116921816173644
  44. 0.0870115841292231
  45. 0.0553710080822329
  46. 0.0295312043105242
  47. 0.012839654048054
  48. 0.00437094605891201
  49. 0.001092736514728
  50. 0.000178405961588244
  51. 1.42724769270596e-05
In [4]:
# Normal distribution

mean <- 5 # mu in literature
sd <- 0.8 # sigma

x <- seq(-5,10,length=1000)
y <- dnorm(x,mean,sd)  #dnorm  : "D"ensity of "NORM"al distribution. This gives the PDF.

plot(x, y, type="n", xlab="Parameter X", ylab="",
     main="Normal Distribution", axes=FALSE)
axis(1, at=seq(0, 10, 2), pos=0)
lines(x, y)
No description has been provided for this image
In [5]:
# Normal distribution
mean=100
sd=15
lb=80      # lower bound of X
ub=120     # upper bound of x   
#lb=mean-1*sd; ub=mean+1*sd
# This will compute the probability for lb < X < ub


x <- seq(-4,4,length=100)*sd + mean
hx <- dnorm(x,mean,sd)  #dnorm  : "D"ensity of "NORM"al distribution

plot(x, hx, type="n", xlab="Parameter X", ylab="",
     main="Normal Distribution", axes=FALSE)

i <- x >= lb & x <= ub
lines(x, hx)
polygon(c(lb,x[i],ub), c(0,hx[i],0), col="red")

area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd)   #pnorm : "P"robability of "NORM" (=Phi(Z))
result <- paste("P(",lb,"< X <",ub,") =",
                signif(area, digits=4))
mtext(result,3)
axis(1, at=seq(40, 160, 20), pos=0)
No description has been provided for this image
In [6]:
# The Whiskey case

1-pnorm(q = 69.5,mean = 67.5,sd = 2.5) 
0.211855398583397
In [ ]: